package com.example.a022.myapplication;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 022 on 2018/9/3.
 */

public class FragmentAdapter extends BaseAdapter {
    private List<String> data;
    private Context context;
    private LayoutInflater layoutInflater;

    public FragmentAdapter(Context context,List<String> data){
        this.data = data;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHodler hodler = null;
        if (convertView == null){
            hodler = new ViewHodler();
            convertView = layoutInflater.inflate(R.layout.home_fragment_item,null);
            hodler.image =(ImageView) convertView.findViewById(R.id.image);
            hodler.name =(TextView) convertView.findViewById(R.id.name);
            hodler.number =(TextView) convertView.findViewById(R.id.number);
            hodler.price = (TextView)convertView.findViewById(R.id.price);
            convertView.setTag(hodler);
        }else {
            hodler = (ViewHodler) convertView.getTag();
        }

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) hodler.image.getLayoutParams();
        params.height = (width-2)/2;
        hodler.image.setLayoutParams(params);

        Log.d("url",data.get(position));
       // Picasso.with(context).load(data.get(position)).into(hodler.image);
       // hodler.name.setText(data.get(position));
        String url = data.get(position);
        Glide.with(context).load(url).centerCrop().placeholder(R.mipmap.ic_launcher).into(hodler.image);

        return convertView;
    }

    private class ViewHodler{
        ImageView image;
        TextView name,price,number;
    }
}
